package com.mudu.search.controller;

import com.mudu.search.entity.DimIndustryIndex;
import com.mudu.search.result.Result;
import com.mudu.search.service.DimIndustryIndexService;
import com.mudu.search.vo.DimIndustryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.data.elasticsearch.core.query.CriteriaQueryBuilder;
import org.springframework.data.elasticsearch.core.query.HighlightQuery;
import org.springframework.data.elasticsearch.core.query.highlight.Highlight;
import org.springframework.data.elasticsearch.core.query.highlight.HighlightField;
import org.springframework.data.elasticsearch.core.query.highlight.HighlightFieldParameters;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * 行业代码控制层
 *
 * @author lzqing
 * @since 2023-03-22 10:46:12
 */
@RestController
@RequestMapping("/api/industry")
@Api(tags = "行业代码管理")
@Validated
public class DimIndustryController {

    @Resource
    private DimIndustryIndexService dimIndustryIndexService;

    @Resource
    private ElasticsearchTemplate elasticsearchTemplate;

    @ApiOperation("同步数据库表记录")
    @GetMapping("/v1/init")
    public Result<String> initIndex() {
        dimIndustryIndexService.initIndex();
        return Result.ok();
    }

    /**
     * 分页查询
     * 带参数
     */
    @ApiOperation("分页查询")
    @PostMapping("/v1/page/list")
    public Result<Page<DimIndustryIndex>> listPageMatch(@RequestBody DimIndustryVO vo) {
        int pageNo = 1;
        int pageSize = 5;

        // contains必须包含完整的关键字，而matches不需要，如果要匹配更多字段使用or或者and。
        Criteria criteria = new Criteria("name").matches(vo.getName()).or(new Criteria("description").matches(vo.getDescription()));
        //高亮查询
        List<HighlightField> highlightFieldList = new ArrayList<>();
        HighlightField highlightField = new HighlightField("name", HighlightFieldParameters.builder().withPreTags("<em>").withPostTags("</em>").build());
        highlightFieldList.add(highlightField);
        highlightField = new HighlightField("description", HighlightFieldParameters.builder().withPreTags("<em>").withPostTags("</em>").build());
        highlightFieldList.add(highlightField);

        Highlight highlight = new Highlight(highlightFieldList);
        HighlightQuery highlightQuery = new HighlightQuery(highlight, DimIndustryIndex.class);
        //构建查询
//        CriteriaQueryBuilder builder = new CriteriaQueryBuilder(criteria).withSort(Sort.by(Sort.Direction.DESC, "industryId")).withSort(Sort.by(Sort.Direction.DESC, "levelType")).withSort(Sort.by(Sort.Direction.DESC, "parentId")).withHighlightQuery(highlightQuery).withPageable(PageRequest.of(0, 10));
        CriteriaQueryBuilder builder = new CriteriaQueryBuilder(criteria).withHighlightQuery(highlightQuery).withPageable(PageRequest.of(0, 10));
        CriteriaQuery query = new CriteriaQuery(builder);
        //通过elasticsearchTemplate查询
        SearchHits<DimIndustryIndex> result = elasticsearchTemplate.search(query, DimIndustryIndex.class);
        //处理结果
        List<SearchHit<DimIndustryIndex>> searchHitList = result.getSearchHits();
        List<DimIndustryIndex> discussPostList = new ArrayList<>();
        for (SearchHit<DimIndustryIndex> hit : searchHitList) {
            DimIndustryIndex industryIndex = hit.getContent();
            //将高亮结果添加到返回的结果类中显示
            var titleHighlight = hit.getHighlightField("name");
            if (titleHighlight.size() != 0) {
                industryIndex.setName(titleHighlight.get(0));
            }
            var contentHighlight = hit.getHighlightField("description");
            if (contentHighlight.size() != 0) {
                industryIndex.setDescription(contentHighlight.get(0));
            }
            discussPostList.add(industryIndex);

        }
        //构建Page对象
        Page<DimIndustryIndex> indexPage = new PageImpl<>(discussPostList, PageRequest.of(9, 10), result.getTotalHits());
        for (DimIndustryIndex index : indexPage) {
            System.out.println(index);
        }
        return Result.ok(indexPage);
    }


}
